package com.logistic.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.logistic.common.Constants;
import com.logistic.dao.HibernateSessionFactory;
import com.logistic.dao.WaybillDao;
import com.logistic.domain.User;
import com.logistic.domain.Waybill;
import com.logistic.page.QueryResult;

public class WaybillDaoImpl extends HibernateDaoSupport implements WaybillDao {
	private Log log = LogFactory.getFactory().getLog(getClass());
	@SuppressWarnings("rawtypes")
	public List getAll() {
		List list = null;
		try{
			Session s = this.getSession();
			String hql = "from User user order by user.Uid asc";
			list = s.createQuery(hql).list();
			if (list.isEmpty()) {
				return null;
			}
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return list;
	}
	
	public User login(User user) {
		User newUser = null;
		try
		{
			Session s = HibernateSessionFactory.getSession();
			Transaction t = s.beginTransaction();
			String hql = "from User user where user.Uemail ='" + user.getUemail().trim() + "' and user.Upassword='" + user.getUpassword().trim() + "'";
			Query q = s.createQuery(hql);
			List list = q.list();
			t.commit();
			if(list.isEmpty() || list.size()==0) {
				return null;
			}
			newUser = (User)list.get(0);
		}catch(HibernateException e){
			e.printStackTrace();
		}finally
		{
			HibernateSessionFactory.closeSession();
		}
		return newUser;
	}
	
	@SuppressWarnings("finally")
	public int save(Waybill wb) {

		Session s = HibernateSessionFactory.getSession();
		Transaction t = s.beginTransaction();
		
		s.saveOrUpdate(wb);
		try {
			t.commit();
		} catch (Exception e) {
			
		} finally {
			HibernateSessionFactory.closeSession();
			return 1;
		}
	}
	
	public int modify(Waybill waybill) {
		String sql = "";
		Session s = HibernateSessionFactory.getSession();
		Transaction t = s.beginTransaction();
		try{
			sql = "update Waybill waybill set ";
			if(waybill.getWbWarehouse() != null && !"".equals(waybill.getWbWarehouse())){
				sql = sql + "waybill.wbWarehouse='"+waybill.getWbWarehouse()+"', ";
			}
			if(waybill.getWbGoodsDesc() != null && !"".equals(waybill.getWbGoodsDesc())){
				//去掉单引号 为了防止这个单引号影响到sql表达
				  String b=new String();
				  String a = waybill.getWbGoodsDesc();
				  int i,j;
				  for(i=0;i<a.length();i++)
				  if(a.charAt(i)!='\'') b=b+a.charAt(i);

				sql = sql + "waybill.wbGoodsDesc='"+b+"', ";
			}
			if(waybill.getWbApplyValue() != null && !"".equals(waybill.getWbApplyValue())){
				sql = sql + "waybill.wbApplyValue='"+waybill.getWbApplyValue()+"', ";
			}
			if(waybill.getWbKouAn() != null && !"".equals(waybill.getWbKouAn())){
				sql = sql + "waybill.wbKouAn='"+waybill.getWbKouAn()+"', ";
			}
			if(waybill.getWbInsuranceFlag() != null && !"".equals(waybill.getWbInsuranceFlag())){
				sql = sql + "waybill.wbInsuranceFlag='"+waybill.getWbInsuranceFlag()+"', ";
			}
			if(waybill.getWbOperateRequireType() != null && !"".equals(waybill.getWbOperateRequireType())){
				sql = sql + "waybill.wbOperateRequireType='"+waybill.getWbOperateRequireType()+"', ";
			}
			if(waybill.getWbTogetherSvc() != null && !"".equals(waybill.getWbTogetherSvc())){
				sql = sql + "waybill.wbTogetherSvc='"+waybill.getWbTogetherSvc()+"', ";
			}
			if(waybill.getWbSplitSvc() != null && !"".equals(waybill.getWbSplitSvc())){
				sql = sql + "waybill.wbSplitSvc='"+waybill.getWbSplitSvc()+"', ";
			}
			if(waybill.getWbSplitNum() != null && !"".equals(waybill.getWbSplitNum())){
				sql = sql + "waybill.wbSplitNum='"+waybill.getWbSplitNum()+"', ";
			}
			if(waybill.getWbPackqcfp() != null && !"".equals(waybill.getWbPackqcfp())){
				sql = sql + "waybill.wbPackqcfp='"+waybill.getWbPackqcfp()+"', ";
			}
			if(waybill.getWbPackyxzy() != null && !"".equals(waybill.getWbPackyxzy())){
				sql = sql + "waybill.wbPackyxzy='"+waybill.getWbPackyxzy()+"', ";
			}
			if(waybill.getWbPackzdwpfx() != null && !"".equals(waybill.getWbPackzdwpfx())){
				sql = sql + "waybill.wbPackzdwpfx='"+waybill.getWbPackzdwpfx()+"', ";
			}
			if(waybill.getWbPackqcnj() != null && !"".equals(waybill.getWbPackqcnj())){
				sql = sql + "waybill.wbPackqcnj='"+waybill.getWbPackqcnj()+"', ";
			}	
			if(waybill.getWbPackjjcl()!= null && !"".equals(waybill.getWbPackjjcl())){
				sql = sql + "waybill.wbPackjjcl='"+waybill.getWbPackjjcl()+"', ";
			}
			if(waybill.getWbPacknjjg() != null && !"".equals(waybill.getWbPacknjjg())){
				sql = sql + "waybill.wbPacknjjg='"+waybill.getWbPacknjjg()+"', ";
			}
			if(waybill.getWbPackghwx() != null && !"".equals(waybill.getWbPackghwx())){
				sql = sql + "waybill.wbPackghwx='"+waybill.getWbPackghwx()+"', ";
			}
			if(waybill.getWbPackjtwx() != null && !"".equals(waybill.getWbPackjtwx())){
				sql = sql + "waybill.wbPackjtwx='"+waybill.getWbPackjtwx()+"', ";
			}
			if(waybill.getWbSendOrderNum() != null && !"".equals(waybill.getWbSendOrderNum())){
				sql = sql + "waybill.wbSendOrderNum='"+waybill.getWbSendOrderNum()+"', ";
			}
			if(waybill.getWbSendOrderNumUSA() != null && !"".equals(waybill.getWbSendOrderNumUSA())){
				sql = sql + "waybill.wbSendOrderNumUSA='"+waybill.getWbSendOrderNumUSA()+"', ";
			}
			if(waybill.getWbStatus() != null && !"".equals(waybill.getWbStatus())){
				sql = sql + "waybill.wbStatus='"+waybill.getWbStatus()+"', ";
			}
			if(waybill.getWbPUcity() != null && !"".equals(waybill.getWbPUcity())){
				sql = sql + "waybill.wbPUcity='"+waybill.getWbPUcity()+"', ";
			}
			if(waybill.getWbAddressList() != null && !"".equals(waybill.getWbAddressList())){
				sql = sql + "waybill.wbAddressList='"+waybill.getWbAddressList()+"', ";
			}
			if(waybill.getWbPUname() != null && !"".equals(waybill.getWbPUname())){
				sql = sql + "waybill.wbPUname='"+waybill.getWbPUname()+"', ";
			}
			if(waybill.getWbPUzip() != null && !"".equals(waybill.getWbPUzip())){
				sql = sql + "waybill.wbPUzip='"+waybill.getWbPUzip()+"', ";
			}
			if(waybill.getWbPUphone() != null && !"".equals(waybill.getWbPUphone())){
				sql = sql + "waybill.wbPUphone='"+waybill.getWbPUphone()+"', ";
			}
			if(waybill.getWbPUaddress() != null && !"".equals(waybill.getWbPUaddress())){
				sql = sql + "waybill.wbPUaddress='"+waybill.getWbPUaddress()+"', ";
			}
			if(waybill.getWbUps() != null && !"".equals(waybill.getWbUps())){
				sql = sql + "waybill.wbUps='"+waybill.getWbUps()+"', ";
			}
			if(waybill.getWbEnterWeight() != null && !"".equals(waybill.getWbEnterWeight())){
				sql = sql + "waybill.wbEnterWeight='"+waybill.getWbEnterWeight()+"', ";
			}
			if(waybill.getWbActualWeight() != null && !"".equals(waybill.getWbActualWeight())){
				sql = sql + "waybill.wbActualWeight='"+waybill.getWbActualWeight()+"', ";
			}
			if(waybill.getWbBalanceDesc() != null && !"".equals(waybill.getWbBalanceDesc())){
				sql = sql + "waybill.wbBalanceDesc='"+waybill.getWbBalanceDesc()+"', ";
			}
			if(waybill.getWbSubmitTime() != null && !"".equals(waybill.getWbSubmitTime())){
				sql = sql + "waybill.wbSubmitTime='"+waybill.getWbSubmitTime()+"', ";
			}
			if(waybill.getWbForecastStatusTime() != null && !"".equals(waybill.getWbForecastStatusTime())){
				sql = sql + "waybill.wbForecastStatusTime='"+waybill.getWbForecastStatusTime()+"', ";
			}
			if(waybill.getWbEnterStatusTime() != null && !"".equals(waybill.getWbEnterStatusTime())){
				sql = sql + "waybill.wbEnterStatusTime='"+waybill.getWbEnterStatusTime()+"', ";
			}
			if(waybill.getWbUnPayforStatusTime() != null && !"".equals(waybill.getWbUnPayforStatusTime())){
				sql = sql + "waybill.wbUnPayforStatusTime='"+waybill.getWbUnPayforStatusTime()+"', ";
			}
			if(waybill.getWbUnPayforStatusTime1() != null && !"".equals(waybill.getWbUnPayforStatusTime1())){
				sql = sql + "waybill.wbUnPayforStatusTime1='"+waybill.getWbUnPayforStatusTime1()+"', ";
			}
			if(waybill.getWbPayforUnSendStatusTime() != null && !"".equals(waybill.getWbPayforUnSendStatusTime())){
				sql = sql + "waybill.wbPayforUnSendStatusTime='"+waybill.getWbPayforUnSendStatusTime()+"', ";
			}
			if(waybill.getWbSendedStatusTime() != null && !"".equals(waybill.getWbSendedStatusTime())){
				sql = sql + "waybill.wbSendedStatusTime='"+waybill.getWbSendedStatusTime()+"', ";
			}
			if(waybill.getWbTotalMoney() != null && !"".equals(waybill.getWbTotalMoney())){
				sql = sql + "waybill.wbTotalMoney='"+waybill.getWbTotalMoney()+"', ";
			}
			if(waybill.getWbNote() != null && !"".equals(waybill.getWbNote())){
				 String b1=new String();
				  String a1 = waybill.getWbNote();
				  int i,j;
				  for(i=0;i<a1.length();i++)
				  if(a1.charAt(i)!='\'') b1=b1+a1.charAt(i);
				sql = sql + "waybill.wbNote='"+b1+"', ";
			}
			if(waybill.getUreceiveId() != null && !"".equals(waybill.getUreceiveId())){
				sql = sql + "waybill.UreceiveId='"+waybill.getUreceiveId()+"', ";
			}
			if(waybill.getWbAllUpsAndwWight() != null && !"".equals(waybill.getWbAllUpsAndwWight())){
				sql = sql + "waybill.wbAllUpsAndwWight='"+waybill.getWbAllUpsAndwWight()+"', ";
			}
			if(waybill.getWbSplitBoxRecord() != null && !"".equals(waybill.getWbSplitBoxRecord())){
				sql = sql + "waybill.wbSplitBoxRecord='"+waybill.getWbSplitBoxRecord()+"', ";
			}
			if(waybill.getWbIsIntegral() != null && !"".equals(waybill.getWbIsIntegral())){
				sql = sql + "waybill.wbIsIntegral='"+waybill.getWbIsIntegral()+"', ";
			}
			
			sql = (String)sql.subSequence(0, sql.length()-2);
			
			Query q = s.createQuery(sql
					+ " where waybill.wbId=?"); 
			q.setParameter(0, waybill.getWbId());
			q.executeUpdate();
			t.commit();

		}catch(HibernateException e){
			log.error(sql+ " where waybill.wbId= '"+ waybill.getWbId() +"'");
			t.rollback();
			e.printStackTrace();
		}finally
		{
			HibernateSessionFactory.closeSession();	
		}
		return 1;
	}
	
	public int modifyByManager(Waybill wb){
	
		Session s = HibernateSessionFactory.getSession();
		Transaction t = s.beginTransaction();
		String sql = "update Waybill waybill set ";
		if(wb.getWbSendOrderNum() != null && !"".equals(wb.getWbSendOrderNum())){
			sql = sql + "waybill.wbSendOrderNum='"+wb.getWbSendOrderNum()+"', ";
		}
		if(wb.getWbStatus() != null && !"".equals(wb.getWbStatus())){
			sql = sql + "waybill.wbStatus='"+wb.getWbStatus()+"', ";
		}
		sql = (String)sql.subSequence(0, sql.length()-2);
		Query q = s.createQuery(sql
				+ " where waybill.wbId=?"); 
		q.setParameter(0, wb.getWbId());
		
		q.executeUpdate();
		try{
			t.commit();
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				HibernateSessionFactory.closeSession();
			}
		return 1;
	}
	
	public void delete(String id) {
		try{
			Session s = HibernateSessionFactory.getSession();
			Transaction tran = s.beginTransaction();
			Query q = s.createQuery("delete from Waybill waybill where waybill.wbId=?");
			q.setParameter(0, id);
			q.executeUpdate();
			tran.commit();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally
		{
			HibernateSessionFactory.closeSession();
		}
	}
	
 
	
	public Waybill getWaybillById(Waybill wb){
		Waybill newWb = null;
		try{
			Session s = HibernateSessionFactory.getSession();
			Transaction t = s.beginTransaction();
			
			String hql = "from Waybill waybill where waybill.wbId =?";
			Query q = s.createQuery(hql);
			q.setParameter(0,wb.getWbId());
			List list = q.list();
			t.commit();
			if(list.isEmpty() || list.size()==0) {
				return null;
			}
			newWb = (Waybill)list.get(0);
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		
		
		return newWb;
	}
	
	public List getWayillByUid(Waybill wb){
		List list = null;
		try{
			Session s = HibernateSessionFactory.getSession();
			Transaction t = s.beginTransaction();
			String hql = "from Waybill waybill where waybill.Uid=?";
			Query q = s.createQuery(hql);
			q.setParameter(0,wb.getUid());
			list = q.list();
			t.commit();
			if(list.isEmpty() || list.size()==0) {
				return null;
			}
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
	
		
		return list;
	}
	
	public List findWaybillByUidAndStatus(Waybill wb){
		List list = null;
		try{
			Session s = HibernateSessionFactory.getSession();
			Transaction t = s.beginTransaction();
			
			String hql = "from Waybill waybill where waybill.Uid =? and waybill.wbStatus = ?";
			Query q = s.createQuery(hql);
			q.setParameter(0, wb.getUid());
			q.setParameter(1, wb.getWbStatus());
			list = q.list();
			t.commit();
			if(list.isEmpty() || list.size()==0) {
				return null;
			}
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return list;
	}
	
	public List findEnterWaybillNum(String Uid){
		List list = null;
		try{
			Session s = HibernateSessionFactory.getSession();
			Transaction t = s.beginTransaction();
			
			String hql = "from Waybill waybill where waybill.Uid =? and waybill.wbStatus = ?";
			Query q = s.createQuery(hql);
			q.setParameter(0, Uid);
			q.setParameter(1, Constants.WB_ENTER_STATUS);
			list = q.list();
			t.commit();
			if(list.isEmpty() || list.size()==0) {
				return null;
			}
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return list;
	}
	
	
	public List searchWaybill(Waybill wb)
	{
		String wbUps = wb.getWbUps();
		String wbIsFastBox = wb.getWbIsFastBox();
		List list = null;
		try{
			Session s = HibernateSessionFactory.getSession();
			Transaction t = s.beginTransaction();
			
			String sql = "from Waybill waybill where  ";
			//不是极速原箱，检查wbUps是否重复的作用
			if(!"Y".equals(wbIsFastBox))
			{
				if(wbUps != null && !"".equals(wbUps)){
					sql = sql + "waybill.wbUps like '"+wbUps+"' and ";
				}
			}
			//极速原箱用来判断 是否已经做了极速原箱的预报
			if("Y".equals(wbIsFastBox))
			{
				if( wbUps != null && !"".equals(wbUps)){
					sql = sql + "waybill.wbUps='"+wb.getWbUps()+"' and ";
				}
				if( wbIsFastBox != null && !"".equals(wbIsFastBox)){
					sql = sql + "waybill.wbIsFastBox='"+wb.getWbIsFastBox()+"' and ";
				}
				//极速原箱都是在未处理里面的
				sql = sql + "waybill.wbStatus='"+Constants.WB_UNHANDLE_STATUS+"' and ";
			}
			
			sql = (String)sql.subSequence(0, sql.length()-5);
			Query q = s.createQuery(sql);
			
			list = q.list();
			t.commit();
			if(list.isEmpty() || list.size()==0) {
				return null;
			}
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return list;
	}
	
	public List searchWaybillForecast(Waybill wb)
	{
		String Uid = wb.getUid();
		String wbUps = wb.getWbUps();
		String wbStatus = wb.getWbStatus();
		String wbId = wb.getWbId();
		String UreceiveId = wb.getUreceiveId();
		String wbPUname = wb.getWbPUname();
		List list = null;
		try{
			Session s = HibernateSessionFactory.getSession();
			Transaction t = s.beginTransaction();
			
			String sql = "from Waybill waybill where  ";
			
			if(Uid != null && !"".equals(Uid)){
				sql = sql + "waybill.Uid='"+Uid+"' and ";
			}
			if(wbId != null && !"".equals(wbId)){
				sql = sql + "waybill.wbId='"+wbId+"' and ";
			}
			if(UreceiveId != null && !"".equals(UreceiveId)){
				sql = sql + "waybill.UreceiveId='"+UreceiveId+"' and ";
			}
			if(wbPUname != null && !"".equals(wbPUname)){
				sql = sql + "waybill.wbPUname='"+wbPUname+"' and ";
			}
			if(wbUps != null && !"".equals(wbUps)){
				sql = sql + "upper(waybill.wbUps) like '%"+wbUps.toUpperCase()+"%' and ";
			}
			if(wbStatus != null && !"".equals(wbStatus)){
				sql = sql + "waybill.wbStatus='"+wbStatus+"' and ";
			}
				sql = (String)sql.subSequence(0, sql.length()-5);
			Query q = s.createQuery(sql);
			
			list = q.list();
			t.commit();
			if(list.isEmpty() || list.size()==0) {
				return null;
			}
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return list;
	}
	
	public List searchByInput(Waybill wb){
		
		String beginTime = wb.getBeginTime();
		String endTime = wb.getEndTime();
		String wbId = wb.getWbId();
		String wbSendOrderNum = wb.getWbSendOrderNum();
		String Uid = wb.getUid();
		String wbStatus = wb.getWbStatus();
		String wbSendOrderNumUSA = wb.getWbSendOrderNumUSA();
		String wbPUname = wb.getWbPUname();
		String wbUps = wb.getWbUps();
		String UreceiveId = wb.getUreceiveId();
		
		List list = null;
		try{
			Session s = HibernateSessionFactory.getSession();
			Transaction t = s.beginTransaction();
			
			String sql = "from Waybill waybill where  ";
			if(wbId != null && !"".equals(wbId)){
				sql = sql + "waybill.wbId='"+wbId+"' and ";
			}
			if(wbPUname != null && !"".equals(wbPUname)){
				sql = sql + "waybill.wbPUname='"+wbPUname+"' and ";
			}
			if(wbSendOrderNum != null && !"".equals(wbSendOrderNum)){
				sql = sql + "waybill.wbSendOrderNum='"+wbSendOrderNum+"' and ";
			}
			if(wbUps != null && !"".equals(wbUps)){
				sql = sql + "waybill.wbUps='"+wbUps+"' and ";
			}
			if(Uid != null && !"".equals(Uid)){
				sql = sql + "waybill.Uid='"+Uid+"' and ";
			}
			if(wbSendOrderNumUSA != null && !"".equals(wbSendOrderNumUSA)){
				sql = sql + "waybill.wbSendOrderNumUSA='"+wbSendOrderNumUSA+"' and ";
			}
			if(wbStatus != null && !"".equals(wbStatus)){
				sql = sql + "waybill.wbStatus='"+wbStatus+"' and ";
			}
			if(UreceiveId != null && !"".equals(UreceiveId)){
				sql = sql + "waybill.UreceiveId='"+UreceiveId+"' and ";
			}
			if(beginTime != null && !"".equals(beginTime) && endTime != null && !"".equals(endTime)){
				sql = sql + "waybill.wbUnPayforStatusTime>='"+beginTime+" 00:00:00' and  waybill.wbUnPayforStatusTime<='"+endTime+" 23:59:59' and " ;
			}
				sql = (String)sql.subSequence(0, sql.length()-5);
			Query q = s.createQuery(sql);
			
			list = q.list();
			t.commit();
			if(list.isEmpty() || list.size()==0) {
				return null;
			}
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return list;
	}
	
	public QueryResult findWaybillsByStatus(String wbArea,String status,int first,int max){
		QueryResult<Waybill> qr = new QueryResult<Waybill>();
		try{
			Session s = HibernateSessionFactory.getSession();
			Transaction t = s.beginTransaction();
			String hql="";
			if(wbArea==null || "".equals(wbArea) ){
				hql =  "from Waybill waybill where waybill.wbStatus = ? order by waybill.wbSubmitTime";
			}else if("CA".equals(wbArea)){
				hql =  "from Waybill waybill where waybill.wbStatus = ? and waybill.wbArea = 'CA' or waybill.wbStatus = ? and waybill.wbArea = 'OR' order by waybill.wbSubmitTime";
			}else{
				hql =  "from Waybill waybill where waybill.wbStatus = ? and waybill.wbArea = '"+wbArea+"' order by waybill.wbSubmitTime";
			}
			Query q = s.createQuery(hql);
			q.setFirstResult(first);
			q.setMaxResults(max);
			q.setParameter(0, status);
			if("CA".equals(wbArea)){
				q.setParameter(1, status);
			}
			List list = q.list();
			try{
				t.commit();
			}catch(Exception e)
			{
				e.printStackTrace();
			}
		
			Query size=null;
			if("CA".equals(wbArea)){
				size = s.createQuery(hql).setParameter(0, status).setParameter(1, status);	
			}else{
				size = s.createQuery(hql).setParameter(0, status);
			}
			
			qr.setTotalrecord(size.list().size());
			qr.setResultlist(list);
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return qr;
	}
	
	public List findWaybillsByStatusWithoutPage(Waybill wb)
	{
		List list = null;
		String status = wb.getWbStatus();
		String wbArea = wb.getWbArea();
		try{
			Session s = HibernateSessionFactory.getSession();
			Transaction t = s.beginTransaction();
			String hql="";
			if(wbArea==null || "".equals(wbArea) ){
				hql =  "from Waybill waybill where waybill.wbStatus = ? order by waybill.wbUps";
			}else if("CA".equals(wbArea)){
				hql =  "from Waybill waybill where waybill.wbStatus = ? and waybill.wbArea = 'CA' or waybill.wbStatus = ? and waybill.wbArea = 'OR' order by waybill.wbUps";
			}else{
				hql =  "from Waybill waybill where waybill.wbStatus = ? and waybill.wbArea = '"+wbArea+"' order by waybill.wbUps";
			}
			Query q = s.createQuery(hql);
			q.setParameter(0, status);
			if("CA".equals(wbArea)){
				q.setParameter(1, status);
			}
			list = q.list();
			try{
				t.commit();
			}catch(Exception e)
			{
				e.printStackTrace();
			}
		
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return list;
	}
	
	
	public QueryResult findCurrentWaybillByUid(String Uid,int first,int max){
		QueryResult<Waybill> qr = new QueryResult<Waybill>();
		try{
			Session s = HibernateSessionFactory.getSession();
			Transaction t = s.beginTransaction();
			
			String hql = "from  Waybill wb where wb.Uid =? and wb.wbStatus != ? and wb.wbStatus != ? and wb.wbStatus != ? and wb.wbStatus != ?";
			//Query q = s.createQuery(hql).setFirstResult(first).setMaxResults(max);
			Query q = s.createQuery(hql);
			q.setFirstResult(first);
			q.setMaxResults(max);
			q.setParameter(0, Uid);
			q.setParameter(1, Constants.WB_ENTER_STATUS);
			q.setParameter(2, Constants.WB_ACCEPTED_STATUS);
			q.setParameter(3, Constants.WB_SENDED_STATUS);
			q.setParameter(4, Constants.WB_FORECAST_STATUS);
			List list = q.list();
			try{
				t.commit();
			}catch(Exception e)
			{
				e.printStackTrace();
			}
			
			
			Query size = s.createQuery(hql);
			size.setParameter(0, Uid);
			size.setParameter(1, Constants.WB_ENTER_STATUS);
			size.setParameter(2, Constants.WB_ACCEPTED_STATUS);
			size.setParameter(3, Constants.WB_SENDED_STATUS);
			size.setParameter(4, Constants.WB_FORECAST_STATUS);
			qr.setTotalrecord(size.list().size());
			
			qr.setResultlist(list);
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return qr;
	}
	
	public QueryResult findHistoryWaybillByUid(String Uid,int first,int max){
		QueryResult<Waybill> qr = new QueryResult<Waybill>();
		try{
			Session s = HibernateSessionFactory.getSession();
			Transaction t = s.beginTransaction();
			
			String hql = "from  Waybill wb where wb.Uid =? and wb.wbStatus = ?";
			//Query q = s.createQuery(hql).setFirstResult(first).setMaxResults(max);
			Query q = s.createQuery(hql);
			q.setFirstResult(first);
			q.setMaxResults(max);
			q.setParameter(0, Uid);
			q.setParameter(1, Constants.WB_SENDED_STATUS);
			List list = q.list();
			try{
				t.commit();
			}catch(Exception e)
			{
				e.printStackTrace();
			}
			Query size = s.createQuery(hql);
			size.setParameter(0, Uid);
			size.setParameter(1, Constants.WB_SENDED_STATUS);
			qr.setTotalrecord(size.list().size());
			
			qr.setResultlist(list);
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return qr;
	}
	
}

